package com.ruoyi.framework.aspectj;



import com.ruoyi.common.annotation.Log;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;
import org.springframework.util.StopWatch;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import java.util.Arrays;
import java.util.Map;


@Aspect
@Component
@Order(100)
public class Log1Aspect {

    @Pointcut("execution(public * com.ruoyi.web.controller..*.*(..))")
    public void webLog() {}

    //  @Before("webLog()")
    @Around("webLog()")
    public Object doBefore(ProceedingJoinPoint joinPoint) {
        // 接收到请求，记录请求内容

        //        System.out.error("WebLogAspect.doBefore()");
        ServletRequestAttributes attributes =
                (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        HttpServletRequest request = attributes.getRequest();
        if(request == null){
            try {
                return joinPoint.proceed();
            } catch (Throwable e) {
                throw new RuntimeException(e);
            }
        }

        // 记录下请求内容

        StopWatch stopWatch = new StopWatch();
        stopWatch.start();

        System.out.println(request.getMethod() + " => URL : " + request.getRequestURI());
        System.out.println(request.getMethod() + " => URL : " + request.getRequestURL().toString());
        System.out.println("IP : " + request.getRemoteAddr());
        System.out.println(
                "CLASS_METHOD : "
                        + joinPoint.getSignature().getDeclaringTypeName()
                        + "."
                        + joinPoint.getSignature().getName());
        final Object[] args = joinPoint.getArgs();
        final String argsStr = Arrays.toString(args);
        System.out.println("ARGS : " + argsStr);

        System.out.println("========params========");

        //    Ualog ualog = new Ualog();
        //    ualog.setCtime(LocalDateTime.now());
        //    ualog.setSmethod(request.getMethod());
        //    ualog.setUrl(request.getRequestURI());
        //    ualog.setClassmethod(
        //        joinPoint.getSignature().getDeclaringTypeName() + "." +
        // joinPoint.getSignature().getName());
        //    ualog.setArgs(argsStr.substring(0, argsStr.length() > 1900 ? 1900 : argsStr.length()));
        //    //        ualog.setIp(request.getRemoteAddr());
        //    ualog.setIp(getIpAddr(request));
        //    ualog.setUser(request.getHeader("satoken"));
        //    ualog.setNname(String.valueOf(getTokenUser()));
        //    ualog.insert();

        Map<String, String[]> map = request.getParameterMap();
        map.forEach(
                (k, v) -> {
                    System.out.println(String.format("[%s] = %s", k, Arrays.toString(v)));
                });

        try {
            return joinPoint.proceed();
        } catch (Throwable throwable) {
            throw new RuntimeException(throwable);
        } finally {
            stopWatch.stop();
            //      long delta = System.currentTimeMillis() - start;
            long delta = stopWatch.getTotalTimeMillis();
            System.err.println(
                    request.getMethod()
                            + " => URL : "
                            + request.getRequestURI()
                            + " => time elapse "
                            + delta
                            + " ms");
            //      System.out.println("time elapse: " + delta + " ms");
        }
    }
}
